package com.Spring.liaojp;

import com.Spring.liaojp.common.PrintInAndOut;
import com.Spring.liaojp.controller.OrderController;
import com.Spring.liaojp.controller.UserController;
import com.Spring.liaojp.entity.Order;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Set;

public class Main {
    public static void main(String[] args) {

        System.out.println("""
                
                 .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .----------------.\s
                | .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
                | |   _____      | || |     _____    | || |      __      | || |     ____     | || |     _____    | || |   ______     | |
                | |  |_   _|     | || |    |_   _|   | || |     /  \\     | || |   .'    `.   | || |    |_   _|   | || |  |_   __ \\   | |
                | |    | |       | || |      | |     | || |    / /\\ \\    | || |  /  .--.  \\  | || |      | |     | || |    | |__) |  | |
                | |    | |   _   | || |      | |     | || |   / ____ \\   | || |  | |    | |  | || |   _  | |     | || |    |  ___/   | |
                | |   _| |__/ |  | || |     _| |_    | || | _/ /    \\ \\_ | || |  \\  `--'  /  | || |  | |_' |     | || |   _| |_      | |
                | |  |________|  | || |    |_____|   | || ||____|  |____|| || |   `.____.'   | || |  `.___.'     | || |  |_____|     | |
                | |              | || |              | || |              | || |              | || |              | || |              | |
                | '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
                 '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'\s
                """);

        // 读取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/*.xml");

        PrintInAndOut.printlnGreen("-----简易订单管理系统-----");
        // 登录
        UserController userController = applicationContext.getBean(UserController.class);

        int login = userController.login();
        if (login == 1){
            return;
        }

        // 进入系统
        // 选择功能
        int choice;
        while (true){
            PrintInAndOut.printlnYellow("1. 订单管理");
            PrintInAndOut.printlnYellow("2. 绣线管理");
            PrintInAndOut.printlnYellow("3. 退出");
            System.out.print("请选择功能：");
            choice = PrintInAndOut.readInt();
            switch (choice){
                case 1:
                {
                    OrderController orderController = applicationContext.getBean(OrderController.class);
                    orderManage(orderController);
                }
                    break;
                case 2:
                    PrintInAndOut.printlnGreen("绣线管理");
                    break;
                case 3:
                    PrintInAndOut.printlnGreen("退出系统");
                    return;
            }
        }
    }

    private static void orderManage(OrderController orderController){

        try {
            int choice;
            while (true) {
                PrintInAndOut.printlnYellow("1. 添加订单");
                PrintInAndOut.printlnYellow("2. 删除订单");
                PrintInAndOut.printlnYellow("3. 修改订单");
                PrintInAndOut.printlnYellow("4. 查询订单");
                PrintInAndOut.printlnYellow("5. 返回上一步");
                System.out.print("请选择功能：");

                choice = PrintInAndOut.readInt();
                switch (choice){
                    case 1:
                    {
                        PrintInAndOut.printlnGreen("添加订单");
                        // todo 实现id自增（Order类引入一个静态变量，setter时增加）
                        PrintInAndOut.printlnYellow("请输入订单id");
                        int id = PrintInAndOut.readInt();
                        PrintInAndOut.printlnYellow("请输入订单需求：" );
                        int need = PrintInAndOut.readInt();
                        PrintInAndOut.printlnYellow("请输入订单价格：");
                        int price = PrintInAndOut.readInt();
                        boolean add = orderController.addOrders(new Order(id, need, price));
                        if (add){
                            PrintInAndOut.printGreen("添加成功");
                        }else {
                            PrintInAndOut.printlnRed("添加失败");
                        }
                        break;
                    }
                    case 2:
                    {
                        PrintInAndOut.printlnGreen("删除订单");
                        System.out.println("删除的订单id：");
                        int id = PrintInAndOut.readInt();
                        orderController.removeOrderById(id);
                        break;
                    }
                    case 3:
                        PrintInAndOut.printlnGreen("修改订单");
                        System.out.println("未实现");
                        break;
                    case 4:
                    {
                        PrintInAndOut.printlnGreen("查询订单");
                        Set<Order> orders = orderController.getOrders();
                        System.out.printf("%-10s %-20s %-30s %-40s %-50s\n", "订单id", "订单需求", "订单总价格", "所需线号", "创建订单的用户");
                        for (Order order : orders){
                            System.out.printf("%-10d %-20d %-30d %-40s %-50s\n", order.getId(), order.getNeed(), order.getPrice()
                                    , order.getEmbroideryFlosses()==null ? "" : order.getEmbroideryFlosses().toString()
                                    , order.getUser()==null ? "" : order.getUser().getName());
                        }
                        break;
                    }
                    case 5:
                        PrintInAndOut.printlnRed("返回上一步");
                        return;
                    default:
                        PrintInAndOut.printlnRed("无效的选择");
                        break;
                }
            }
        } finally {
            closeResource();
        }
    }

    /**
     * 关闭资源
     */
    private static void closeResource(){
        // scanner关闭
        PrintInAndOut.scanner.close();
    }
}

